<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use zgldh\QiniuStorage\QiniuStorage;//引入文件存储类
use File;//引入文件操作类
use Input;
class UploadController extends Controller
{
    //默认的上传操作方法
    public function img_default(Request $request){
    	//判断文件上传的是否成功
    	if($request -> hasFile('file') && $request -> file('file') -> isValid()){
    		//上传成功
    		$new = md5(time() . $request -> file('file') -> getClientOriginalName()) . '.' . $request -> file('file') -> getClientOriginalExtension();
    		//echo $new;
    		//文件保存
    		Storage::disk('public') -> put($new,File::get($request -> file('file') -> path()));
    		//返回数据
    		return response() -> json([
    				'status'	=>	'1',
    				'message'	=>	'上传成功',
    				'path'		=>	'/storage/' . $new
    				///storage/07126584f1e8be280585ecf4ebda569f.png
    			]);
    	}else{
    		//上传失败（0-7，没有5）
    		return response() -> json([
    				'status'	=>	'0',
    				'message'	=>	'上传失败'
    			]);
    	}
    }

    //七牛专用
    public function qiniu(Request $request){
    	//判断文件上传的是否成功
    	if($request -> hasFile('file') && $request -> file('file') -> isValid()){
			$qiniu_type = Input::get('qiniu_type');
			if(empty($qiniu_type) == true){
				$data = array(
					'status'  => '0',
					'msg'     => '参数错误'
				);
				return response() -> json($data);
			}
			$disk = QiniuStorage::disk($qiniu_type);
			$type =  Input::get('img_type');

			if(empty(session($type.'_url')) == false){
				if($disk->exists(session($type.'_url')) == true){
                        //删除上一张图片
                    $result = $disk->delete(session($type.'_url'));
                    if($result == false){
                        $data = array(
                            'status'	=>	'0',
                            'message'	=>	'删除原文件失败',
                        );
                        return response() -> json($data);
                    }
                }
			}
			//上传成功
    		$new = md5(time() . $request -> file('file') -> getClientOriginalName()) . '.' . $request -> file('file') -> getClientOriginalExtension();
    		//文件保存
            $result = $disk -> put($new,File::get($request -> file('file') -> path()));
			if($result == true){
                session([$type.'_url' => $new]);
			    $data = array(
                    'status'	=>	'1',
                    'message'	=>	'上传成功',
                    'path'		=>	$disk -> downloadUrl($new)
                );
            }else{
                $data = array(
                    'status'	=>	'0',
                    'message'	=>	'上传失败',
                );
            }
    		//返回数据
    		return response() -> json($data);
    	}else{
    		//上传失败（0-7，没有5）
    		return response() -> json([
    				'status'	=>	'0',
    				'message'	=>	'上传失败'
    			]);
    	}
    }

	public function video(Request $request){
		//判断文件上传的是否成功

		if($request -> hasFile('file') && $request -> file('file') -> isValid()){
			$disk = QiniuStorage::disk('video');

			$type =  Input::get('video_type');
			if(strtolower($request -> file('file') -> getClientOriginalExtension()) != 'mp4'){
				$data = array(
						'status'	=>	'0',
						'message'	=>	'文件类型错误',
				);
				return response() -> json($data);
			}
			if(empty(session($type.'_url')) == false){
				if($disk->exists(session($type.'_url')) == true){
					//删除
					$result = $disk->delete(session($type.'_url'));
					if($result == false){
						$data = array(
								'status'	=>	'0',
								'message'	=>	'删除原文件失败',
						);
						return response() -> json($data);
					}
				}
			}

			//上传成功
			$new = md5(time() . $request -> file('file') -> getClientOriginalName()) . '.' . $request -> file('file') -> getClientOriginalExtension();
			//文件保存
			$result = $disk -> put($new,File::get($request -> file('file') -> path()));
			if($result == true){
				session([$type.'_url' => $new]);
				$data = array(
						'status'	=>	'1',
						'message'	=>	'上传成功',
						'path'		=>	$disk -> downloadUrl($new)
				);
			}else{
				$data = array(
						'status'	=>	'0',
						'message'	=>	'上传失败1',
				);
			}
			//返回数据
			return response() -> json($data);
		}else{
			//上传失败（0-7，没有5）
			return response() -> json([
					'status'	=>	'0',
					'message'	=>	'上传失败'
			]);
		}
	}

}
